package com.zrt.view;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

@Controller
public class Management extends JFrame implements ActionListener {
    JMenuBar jmb;
    JMenu jm1, jm4;
    JMenuItem jmi1, jmi2, jmi3, jmi4;
    JLabel jl1, jl2, jl3;
    JTable table, table1;
    JScrollPane jsp, jsp1;
    Vector linename, linename1;
    Vector data, data1;
    JPanel jp1, jp2, jp3, jp4, jp5, jp6, jp7, jp8;
    JButton jb1, jb2, jb3, jb4, jb5, jb6, jb7, jb8, jb9;
    JTextField jtf, jtf1;
    int flag = 100;
    int i = 0;


    @Autowired
    private Assets assets;
    @Autowired
    private Settle settle;
    @Autowired
    private AddTeacher addTeacher;
    @Autowired
    private ModifyTeacher modifyTeacher;
    @Autowired
    private AddStudent addStudent;
    @Autowired
    private ModifyStudent modifyStudent;


    public void myManagement() {

        jmb = new JMenuBar();
        jmb.setBackground(Color.white);


        jm1 = new JMenu("选 择");
        jm1.setFont(new Font("宋体", Font.BOLD, 22));
        jm1.setMnemonic('F');
        jmb.add(jm1);

        jm4 = new JMenu("设 置");
        jm4.setMnemonic('S');
        jm4.setFont(new Font("宋体", Font.BOLD, 22));
        jmb.add(jm4);

        jl1 = new JLabel("                        总资产:");
        jl1.setFont(new Font("宋体", Font.BOLD, 22));
        jl1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        jl1.setToolTipText("点击刷新");
        jmb.add(jl1);

        jmi1 = new JMenuItem("资 产");
        jmi1.setFont(new Font("宋体", Font.BOLD, 20));
        jmi1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                assets.myAssets();
            }
        });
        jm4.add(jmi1);

        jmi4 = new JMenuItem("结 算");
        jmi4.setFont(new Font("宋体", Font.BOLD, 20));
        jmi4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                settle.mySettle();
            }
        });
        jm4.add(jmi4);


        jmi2 = new JMenuItem("教 师");
        jmi2.setFont(new Font("宋体", Font.BOLD, 20));
        jmi2.addActionListener(this);
        jm1.add(jmi2);

        jmi3 = new JMenuItem("学 生");
        jmi3.setFont(new Font("宋体", Font.BOLD, 20));
        jmi3.addActionListener(this);
        jm1.add(jmi3);

        linename = new Vector();
        linename.add("工号");
        linename.add("名字");
        linename.add("性别");
        linename.add("年龄");
        linename.add("职位");
        linename.add("固定工资");
        linename.add("课时费");
        linename.add("工资");

        data = new Vector();
        Vector row = new Vector();
//        //用row.add（） 添加数据;
//
        data.add(row);
        table = new JTable(data, linename);

        jsp = new JScrollPane(table);
//
        jb1 = new JButton("添加");
        jb1.setFont(new Font("宋体", Font.BOLD, 20));
        jb1.addActionListener(new ActionListener() {
            @Override
            //添加老师
            public void actionPerformed(ActionEvent e) {
                addTeacher.myAddProfessor();
            }
        });

        jb2 = new JButton("修改");
        jb2.setFont(new Font("宋体", Font.BOLD, 20));
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int i = table.getSelectedRow();
                if (i == -1){
                    JOptionPane.showMessageDialog(null,"请选择ni要修改的行");
                    return;
                }
//                暂时注释
//                modifyTeacher.myModifyTeacher();
            }
        });

        jb3 = new JButton("删除");
        jb3.setFont(new Font("宋体", Font.BOLD, 20));
        jb3.addActionListener(new ActionListener() {
            @Override
            //删除
            public void actionPerformed(ActionEvent e) {
                int i = table.getSelectedRow();
                if (i == -1){
                    JOptionPane.showMessageDialog(null,"请选择要删除的行");
                    return;
                }

                Integer id = (Integer) table.getValueAt(i,0);
                //id 是你点击列表要删除的那个人的id
            }
        });

        jtf = new JTextField(15);
        jtf.setFont(new Font("宋体", Font.BOLD, 20));


        jb4 = new JButton("查询");
        jb4.setFont(new Font("宋体", Font.BOLD, 20));
        jb4.addActionListener(new ActionListener() {
            @Override
            //查询
            public void actionPerformed(ActionEvent e) {

            }
        });


        jp1 = new JPanel();
        jp1.setLayout(new BorderLayout());
        jp1.add(jmb, BorderLayout.NORTH);

        jp2 = new JPanel();
        jp2.setLayout(new BorderLayout());
        jp2.add(jsp, BorderLayout.CENTER);

        jp3 = new JPanel();
        jp3.setLayout(new FlowLayout());
        jp3.add(jb1);
        jp3.add(jb2);
        jp3.add(jb3);

        jp4 = new JPanel();
        jp4.setLayout(new FlowLayout());
        jp4.add(jb4);
        jp4.add(jtf);

        jp2.add(jp3, BorderLayout.SOUTH);
        jp2.add(jp4, BorderLayout.NORTH);


        linename1 = new Vector();
        linename1.add("学号");
        linename1.add("名字");
        linename1.add("性别");
        linename1.add("年龄");
        linename1.add("类别");
        linename1.add("每小时酬劳");
        linename1.add("每月酬劳");
        linename1.add("学费");

        data1 = new Vector();
        Vector row1 = new Vector();
        //用row.add（） 添加数据;

        data1.add(row1);
        table1 = new JTable(data1, linename1);

        jsp1 = new JScrollPane(table1);

        jb5 = new JButton("添加");
        jb5.setFont(new Font("宋体", Font.BOLD, 20));
        jb5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                addStudent.myAddStudent();
            }
        });

        jb6 = new JButton("修改");
        jb6.setFont(new Font("宋体", Font.BOLD, 20));
        jb6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int i = table.getSelectedRow();
                if (i == -1){
                    JOptionPane.showMessageDialog(null,"请选择要修改的行");
                    return;
                }
//                暂时注释
//                modifyStudent.myModifyStudent();
            }
        });

        jb7 = new JButton("删除");
        jb7.setFont(new Font("宋体", Font.BOLD, 20));
        jb7.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int i = table.getSelectedRow();
                if (i == -1){
                    JOptionPane.showMessageDialog(null,"请选择要删除的行");
                    return;
                }

            }
        });

        jb9 = new JButton("评估");
        jb9.setFont(new Font("宋体", Font.BOLD, 20));
        jb9.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

        jtf1 = new JTextField(15);
        jtf1.setFont(new Font("宋体", Font.BOLD, 20));


        jb8 = new JButton("查询");
        jb8.setFont(new Font("宋体", Font.BOLD, 20));
        jb8.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

        // 中间
        jp5 = new JPanel();
        jp5.setLayout(new BorderLayout());
        jp5.add(jsp1, BorderLayout.CENTER);

        jp6 = new JPanel();
        jp6.setLayout(new FlowLayout());
        jp6.add(jb5);
        jp6.add(jb6);
        jp6.add(jb7);
        jp6.add(jb9);

        jp5.add(jp6, BorderLayout.SOUTH);

        jp7 = new JPanel();
        jp7.setLayout(new FlowLayout());
        jp7.add(jb8);
        jp7.add(jtf1);

        jl2 = new JLabel(new ImageIcon(getClass().getResource("/images/4.png")));
        jp8 = new JPanel();
        jp8.add(jl2);


        jp5.add(jp6, BorderLayout.SOUTH);
        jp5.add(jp7, BorderLayout.NORTH);

        add(jp1, BorderLayout.NORTH);
//        add(jp2, BorderLayout.CENTER);
        add(jp8);


        setTitle("电子科技大学成都学院财务管理系统");
        setSize(750, 550);
        setLocation(400, 100);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {


        //学生 1
        if (e.getSource() == jmi3) {
            flag = 1;

            if (i == 0 && flag == 1) {
                Container pane = jp8.getRootPane().getContentPane();
                pane.remove(jp8);
                pane.add(jp5);
                validate();//重构界面
                repaint();//重新绘制
                i++;
                flag = 100;
            }

            if (i > 0 && flag == 1) {
                Container pane = jmb.getRootPane().getContentPane();
                pane.remove(jp2);
                pane.add(jp5);
                validate();//重构界面
                repaint();//重新绘制
                flag = 1;
            }
        }


        //教师 0
        if (e.getSource() == jmi2) {

            flag = 0;

            if (i == 0 && flag == 0) {
                Container pane = jp8.getRootPane().getContentPane();
                pane.remove(jp8);
                pane.add(jp2);
                validate();//重构界面
                repaint();//重新绘制
                i++;
            }

            if (i > 0 && flag == 0) {
                Container pane = jmb.getRootPane().getContentPane();
                pane.remove(jp5);
                pane.add(jp2);
                validate();//重构界面
                repaint();//重新绘制
                flag = 100;

            }

        }

    }
}